#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Agreements
{
	/// <summary>
	/// Summary description for Fund.
	/// </summary>
	public class Fund : BO.BusinessObject
	{
		#region Constants
		private static string FundKey = GenerateKey.MakeKey("Fund_4854D36D-D369-4676-8CD6-3EF56D055D88",GenerateKey.Agreement);
		#endregion

		#region Constructors
		/// <summary>
		/// Initializes the datacontainer with the one passed in.
		/// </summary>
		/// <param name="dataSet">The dataset to be used by the object.</param>
		/// <param name="cachePolicy">Prevalent cache policy for object session persistence.</param>
		public Fund(DataSet dataSet, ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = dataSet;
		}

		/// <summary>
		///  Initializes the data container to a new instance of a dataset..
		/// </summary>
		/// <param name="cachePolicy">Prevalent cache policy for object session persistence.</param>
		public Fund(ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = new DataSet();
		}
		#endregion

		#region private data members
		private const string FundDataTableName = "Fund";
		private const string FundDetailsDataTableName = "FundDetails";
		#endregion

		#region Properties
		/// <summary>
		/// Fund Number
		/// </summary>
		public string FundNumber
		{
			get
			{
				object o = DataContainer.Tables[FundDataTableName].Rows[0]["FundNumber"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}

		/// <summary>
		/// AgrFundType Name, readonly
		/// </summary>
		public string FundType
		{
			get
			{
				object o = DataContainer.Tables[FundDataTableName].Rows[0]["FundType"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}

		/// <summary>
		/// Read only string Fund Sub Type.
		/// </summary>
		public string FundSubType
		{
			get 
			{ 
				object o = DataContainer.Tables[FundDataTableName].Rows[0]["FundSubType"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}

		/// <summary>
		/// Account Cycle type of the Agreement, read only currently.
		/// Lookup to FinAccountCycleType table
		/// </summary>
		public string AccountCycleType
		{
			get 
			{
				object o = DataContainer.Tables[FundDataTableName].Rows[0]["AccountCycleType"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}

		/// <summary>
		/// Cost Center Number comes from teh BICS/Walker systems and is the
		/// legacy account number. It is read-only.
		/// </summary>
		public string CostCenterNumber
		{
			get
			{
				object o = DataContainer.Tables[FundDataTableName].Rows[0]["CostCenterNumber"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}

        /// <summary>
        /// Get the activation date for this fund.
        /// </summary>
        public string ActivationDate
        {
            get
            {
                object o = DataContainer.Tables[FundDataTableName].Rows[0]["ActivationDate"];
                return (o == DBNull.Value) ? "" : ((DateTime)o).ToShortDateString();
            }
        }

        /// <summary>
        /// Get the inactivation date for this fund.
        /// </summary>
        public string InactivationDate
        {
            get
            {
                object o = DataContainer.Tables[FundDataTableName].Rows[0]["InactivationDate"];
                return (o == DBNull.Value) ? "" : ((DateTime)o).ToShortDateString();
            }
        }

        /// <summary>
        /// Get the inactivation date for this fund.
        /// </summary>
        public string DocumentId
        {
            get
            {
                object o = DataContainer.Tables[FundDataTableName].Rows[0]["DocumentId"];
                return (o == DBNull.Value) ? "" : (string)o;
            }
        }

        /// <summary>
        /// Fund Number and MRN(Medical Record Number)
        /// </summary>
        public string FNumberAndMRN
        {
            get
            {
                object o = DataContainer.Tables[FundDataTableName].Rows[0]["FNumberAndMRN"];
                return (o == DBNull.Value) ? "" : (string)o;
            }
        }
		#endregion

		#region Base Methods
		public override void Cache()
		{
			CachePolicy.Cache(FundKey, this);
		}

		public override void UnCache()
		{
			CachePolicy.UnCache(FundKey);
		}


        
		/// <summary>
		/// Gets project details (PI name, sponsor, project title, etc) for a given fund number 
		/// to support the fund lookup functionality OR fund information (fund type, acct cycle type, etc) 
		/// for a fund id. The method returns fund information by default (if only one argument, fundid, is passed in).
		/// </summary>
		/// <param name="connection">string used to create connection to the data store.</param>
		/// <param name="args">argument list.</param>
		public override void Load( string connectionString, params object[] args )
		{
			if (args.Length == 0)
				throw new Exception ("Fund Load expects at least one argument." );
			try
			{
				if (args.Length == 2 && args[1] is FundCommandType && (FundCommandType)args[1] == FundCommandType.GetFundDetails)
					Utility.DataAccess.LoadDataSet(DataContainer, FundDetailsDataTableName, connectionString,
						"GetFundDetails", Utility.DataAccess.BuildSqlParameterArray("@fundNumber", SqlDbType.VarChar, (string)args[0]));
				else
					Utility.DataAccess.LoadDataSet(DataContainer, FundDataTableName, connectionString,
						"GetFund", Utility.DataAccess.BuildSqlParameterArray("@fundId", SqlDbType.Int, (int)args[0]));
			}
			catch( Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog ("Fund Load", e, true, Utility.ExceptionReport.ReportType.Error);
			}
		}
		#endregion
	}

	/// <summary>
	/// Used in Load method to determine the stored procedure to invoke.
	/// </summary>
	public enum FundCommandType 
	{
		/// <summary>Gets details for a fund. </summary>
		GetFund,
		/// <summary> Gets fund project details.  </summary>
		GetFundDetails
	}
}
